<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="db.ReceiveorderDAO"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="db.ProjectDAO"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.SubstoreDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp"%>
<%
	WebApplicationContext ctx = WebApplicationContextUtils
			.getWebApplicationContext(getServletContext());
	ReceiveorderDAO receiveorderDao = (ReceiveorderDAO) ctx
			.getBean("ReceiveorderDAO");
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO) ctx
			.getBean("ReceivepurmatDAO");
	SubstoreDAO substoreDao = (SubstoreDAO)ctx.getBean("SubstoreDAO");
	ProjectDAO projectDao = (ProjectDAO)ctx.getBean("ProjectDAO");
	
	String searchSub = request.getParameter("searchSub");
	String searchType = request.getParameter("searchType");
	String searchKeyword = request.getParameter("searchKeyword");
	
	String[][] fields = new String[][]{new String[]{"","id"},new String[]{"","m.code"},new String[]{"","m.name"},new String[]{"","batch"},new String[]{"","m.company"},new String[]{"","useNum"},new String[]{"","subStore"},new String[]{"","pr.code"}};;
	String[] join = new String[]{"place","material","substore","project"};
	
	int pn = 0;
	int pageSize = 0;
	Integer teamId = (Integer)session.getAttribute("tid");
	
	int type = 1;
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "isDelete", 0});
	where.add(new Object[]{"eq", "teamId", teamId});
	
	if(searchSub != null && !"".equals(searchSub)){
		if(!"0".equals(searchSub))
			where.add(new Object[]{"eq", "subStore", Integer.parseInt(searchSub)});
		
		if("2".equals(searchSub)){
			fields = new String[][]{new String[]{"","id"},new String[]{"","m.code"},new String[]{"","m.name"},new String[]{"","batch"},new String[]{"","m.company"},new String[]{"","useNum"},new String[]{"","subStore"}};
			join = new String[]{"place","material","substore"};
		}
	}
	
	if(searchType != null && !"".equals(searchType)){
		if("1".equals(searchType))
			where.add(new Object[]{"like", "m.code", searchKeyword});
		else if("2".equals(searchType))
			where.add(new Object[]{"like", "batch", searchKeyword});
		else if("3".equals(searchType))
			where.add(new Object[]{"like", "m.name", searchKeyword});
		else if("4".equals(searchType))
			where.add(new Object[]{"like", "proCode", "%" + searchKeyword + "%"});
	}
	
	Map map = receivepurmatDao.getReceivepurmat(pn, pageSize,
				new String[][]{new String[]{"desc","date"}},
				where,fields,join);
	request.setAttribute("map",map);
	
	request.setAttribute("substores",substoreDao.findAll());
	request.setAttribute("projects",projectDao.findAll());
	
	request.setAttribute("searchSub",searchSub);
	request.setAttribute("searchType",searchType);
	request.setAttribute("searchKeyword",searchKeyword);

	pageContext.forward("/vm/transfer/stockTransfer.vm");
%>